package com.cloudsigns.factorymanager2;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.control.activity.ControlFragmentActivity;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.ILoginView;
import com.cloudsigns.factorymanager2.engineering.activity.BaseActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringMainForManagerActivity;


import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.engineering.presenter.LoginPresenter1;
import com.cloudsigns.factorymanager2.project.ProjectMainActivity;
import com.cloudsigns.factorymanager2.project.customview.EditTextWatcher;
import com.cloudsigns.factorymanager2.project.myinerface.IEditTextChangeListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

/**
 * Created by tony on 2017/11/15.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener, IEditTextChangeListener, ILoginView {
    private static final String TAG = "LoginActivity";
    private ImageView img_title_finish;
    private TextView tv_title;
    private EditText edt_account, edt_password;
    private ImageView img_pwd_state;
    private TextView tv_phone_register, tv_forget_pwd;
    private /*TextView*/ Button btn_save;
//    private LoginPresenter loginPresenter;
    private LoginPresenter1 loginPresenter;
    private String account = "";

    private boolean pwdState;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_login);

//        loginPresenter = new LoginPresenter(this);
        loginPresenter = new LoginPresenter1(this);

        init();
    }

    private void init() {
        img_title_finish = findViewById(R.id.img_title_finish);
        tv_title = findViewById(R.id.tv_title);
        edt_account = findViewById(R.id.edt_account);
        edt_password = findViewById(R.id.edt_password);
        img_pwd_state = findViewById(R.id.img_pwd_state);
        tv_phone_register = findViewById(R.id.tv_phone_register);
        tv_forget_pwd = findViewById(R.id.tv_forget_pwd);
        btn_save = findViewById(R.id.btn_save);

        EditTextWatcher editTextWatcher = new EditTextWatcher();
        editTextWatcher.setEditTextChangeListener(this);
        editTextWatcher.addAllEditText(edt_account, edt_password);

        img_title_finish.setOnClickListener(this);
        img_pwd_state.setOnClickListener(this);
        tv_phone_register.setOnClickListener(this);
        tv_forget_pwd.setOnClickListener(this);
        btn_save.setOnClickListener(this);

        tv_title.setText(getResources().getText(R.string.ybs_title));

        img_title_finish.setVisibility(View.VISIBLE);

    }

    private boolean isEmpty() {
        if (TextUtils.isEmpty(edt_account.getText().toString().trim())) {
            Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show();
            return true;
        }

        if (TextUtils.isEmpty(edt_password.getText().toString().trim())) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }

    @Override
    public void onTextChange(boolean isChange) {
//        if (isChange)
//            btn_save.setBackgroundResource(R.drawable.btn_click_login_pressed);
//        else
//            btn_save.setBackgroundResource(R.drawable.btn_click_login_normal);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title_finish:
                finish();
                break;
            // 密码显示与隐藏
            case R.id.img_pwd_state:
                if (pwdState) {
                    img_pwd_state.setImageDrawable(getResources().getDrawable(R.mipmap.pwd_invisible));
                    edt_password.setTransformationMethod(PasswordTransformationMethod.getInstance());// 隐藏
                } else {
                    img_pwd_state.setImageDrawable(getResources().getDrawable(R.mipmap.pwd_visible));
                    edt_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());// 显示
                }
                pwdState = !pwdState;
                break;
            // 注册
            case R.id.tv_phone_register:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            // 忘记密码
            case R.id.tv_forget_pwd:
                break;
            // 登录
            case R.id.btn_save:
                if (!isEmpty())
//                    loginPresenter.login();
                    loginPresenter.login();



//                save();
                break;
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case ConstantUtil.LOGIN_SUCCESS:
                    Intent mIntent = new Intent(LoginActivity.this, EngineeringMainForManagerActivity.class);
                    mIntent.putExtra("flag", 2);
                    startActivity(mIntent);
                    Toast.makeText(LoginActivity.this, "恭喜你，登陆成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case ConstantUtil.LOGIN_FAILED:
                    Toast.makeText(LoginActivity.this, "登陆失败" + msg.obj, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    private void save() {
        final String account = edt_account.getText().toString().trim();
        final String pwd = edt_password.getText().toString().trim();

        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
            return;

        }


    }


    @Override
    public String getUserName() {
        account = edt_account.getText().toString().trim();
        return account;
//        return "13800138000";
    }

    @Override
    public String getPassword() {
//        return "987654321";
        return edt_password.getText().toString().trim();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void toMainActivity() {
        Intent mIntent = null;

        AccountInfoBean accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        if (null != accountInfoBean) {
//            if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.SUPER) {
            if (accountInfoBean.getRoleName() == CommonEnum.RoleName.SysManager) {
                mIntent = new Intent(this, ProjectMainActivity.class);// 项目
//                mIntent = new Intent(this, EngineeringMainForSuperActivity.class);

//                  mIntent = new Intent(this, OperateMaintainFragmentActivity.class);// 运维-运营层

//                mIntent = new Intent(this, ControlFragmentActivity.class);// 管控
            }

//            else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {
//                mIntent = new Intent(this, EngineeringMainForManagerActivity.class);// 工程
////                mIntent = new Intent(this, ProduceMainActivity.class);// 生产
////                mIntent = new Intent(this, MediaSystemMainFragmentActivity.class);// 媒体运维系统
////                mIntent = new Intent(this,OperateHighLevelFragmentActivity.class);// 运维-高层
////                mIntent = new Intent(this,OperateMiddleLevelFragmentActivity.class);// 运维-中层
//
//            } else if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.TEAM) {
//                mIntent = new Intent(this, TeamEngineeringMainActivity.class);// 工程班组
////                mIntent = new Intent(this, ProduceTeamMainActivity.class);// 生产班组
//
//            }
            startActivity(mIntent);
            finish();
        }
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }
}
